﻿using System.Collections.Generic;
using System.Linq;
using Xoohoo.Infrastructure;
using Xoohoo.Models;

namespace Xoohoo.Extensions
{
    public static class ModuleSubMenuItemCollectionExtensions
    {
        public static void RemoveByPermission(this List<ModuleSubMenuItem> items, IUser user)
        {
            if (items.IsNullOrEmpty()) return;
            if (user == null) items.Clear();

            items.RemoveAll(it => !user.HasPermission(it.Permission));

            var itemGroup = from p in items
                            where p is ModuleSubMenuItemGroup
                            select p as ModuleSubMenuItemGroup;

            foreach (var it in itemGroup)
            {
                RemoveByPermission(it.Items, user);
            }
        }

    }
}
